﻿#region Librerias
using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
#endregion

/// <summary>
/// Class Publicity, dependiente de la clase Person, define una persona natural
/// </summary>
//tratamiento especial para AGE
#region Class

public class clsPublicity : DAC {
    #region Variables

    private int idPublicity, idPLegal, idWorkArea;
    private String sName, sUrlImage, dtDate, sRegion;
    private const String tabla = "dbo.tblPublicity";

    #endregion

    #region Builder

    /// <summary>
    /// Constructor
    /// </summary>
    public clsPublicity() { }

    #endregion

    #region Methods

    #region Consultations

    public DataSet SeeAllTheRecords() {
        String query = "SELECT row_number() over(order by " + tabla + ".idPublicity) As NUMBER, ";
        query += tabla + ".idPublicity As ID, " + tabla + ".idPLegal As LEGAL, ";
        query += tabla + ".sName As NAME, " + tabla + ".sUrlImage As URLImage, ";
        query += tabla + ".dtDate As DATE, " + tabla + ".idWorkArea As RUBRO, " + tabla + ".sRegion As REGION ";
        query += "FROM " + tabla;
        return this.ExecuteQuery(query);
    }

    public DataSet SearchRegionRubro(string region, string rubro){
        String query = "SELECT row_number() over(order by " + tabla + ".idPublicity) As NUMBER, ";
        query += tabla + ".idPublicity As ID, ";
        query += tabla + ".sName As NAME, " + tabla + ".sUrlImage As URLIMAGE, ";
        query += tabla + ".dtDate As DATE ";
        query += "FROM " + tabla;        
        query += " WHERE " + tabla + ".sRegion='" + region + "' ";
        if (!rubro.Equals("")) {
            query += "AND " + tabla + ".idWorkArea=" + rubro;
        }
        return this.ExecuteQuery(query);
        
    }

    public DataSet SeeAllTheRecords(int startRecord, int maxRecord, String text){
        String query = "SELECT row_number() over(order by " + tabla + ".idPublicity) As NUMBER, ";
        query += tabla + ".idPublicity As ID, " + tabla + ".idPLegal As LEGAL, ";
        query += tabla + ".sName As NAME, " + tabla + ".sUrlImage As URLImage, ";
        query += tabla + ".dtDate As DATE, " + tabla + ".idWorkArea As RUBRO, " + tabla + ".sRegion As REGION ";
        query += "FROM " + tabla;
        if(!text.Equals("")){
            query += " WHERE " + tabla + ".sName LIKE '%" + text + "%' ";
        }
        if (startRecord == 0 && maxRecord == 0) {
            return this.ExecuteQuery(query);
        }
        else {
            return this.ExecuteQuery(query, startRecord, maxRecord);
        }        
    }

    public DataSet ConsultID(String id) {
        String query = "SELECT ";
        query += tabla + ".idPublicity As ID, " + tabla + ".idPLegal As LEGAL, ";
        query += tabla + ".sName As NAME, " + tabla + ".sUrlImage As URLImage, ";
        query += tabla + ".dtDate As DATE, " + tabla + ".idWorkArea As RUBRO, " + tabla + ".sRegion As REGION ";
        query += "FROM " + tabla;
        query += " WHERE " + tabla + ".idPublicity =" + id;
        return this.ExecuteQuery(query);
    }

    public DataSet ConsultPLegalID(String id) {
        String query = "SELECT ";
        query += tabla + ".idPublicity As ID, " + tabla + ".idPLegal As LEGAL, ";
        query += tabla + ".sName As NAME, " + tabla + ".sUrlImage As URLImage, ";
        query += tabla + ".dtDate As DATE, " + tabla + ".idWorkArea As RUBRO, " + tabla + ".sRegion As REGION ";
        query += "FROM " + tabla;
        query += " WHERE " + tabla + ".idPLegal=" + id;
        return this.ExecuteQuery(query);
    }

    public DataSet ConsultPublicityID(string id){
        String query = "SELECT ";
        query += tabla + ".idPublicity As ID, " + tabla + ".idPLegal As LEGAL, ";
        query += tabla + ".sName As NAME, " + tabla + ".sUrlImage As URLImage, ";
        query += tabla + ".dtDate As DATE, " + tabla + ".idWorkArea As RUBRO, " + tabla + ".sRegion As REGION ";
        query += "FROM " + tabla;
        query += " WHERE " + tabla + ".idWorkArea=" + id;
        return this.ExecuteQuery(query);
    }

    #endregion

    public int Insert() {
        String query = "INSERT INTO " + tabla;
        query += "(idPLegal, sName, sUrlImage, dtDate, idWorkArea, sRegion) ";
        query += "VALUES ('{0}','{1}','{2}','{3}','{4}','{5}') SELECT CAST(scope_identity() AS int)";

        String sql = String.Format(query,this.IdPLegal, this.SName, this.SUrlImage, this.DtDate, this.IdWorkArea, this.SRegion);
        return this.ExecuteDML(sql, "no");
    }

    public int Update(){
        String query = "UPDATE " + tabla + " SET ";        
        query += tabla + ".idPLegal=" + this.IdPLegal + ", " + tabla + ".sName='" + this.SName + "', ";
        query += tabla + ".sUrlImage='" + this.SUrlImage + "', " + tabla + ".dtDate='" + this.DtDate + "', ";
        query += tabla + ".idWorkArea=" + this.IdWorkArea + ", " + tabla + ".sRegion='" + this.SRegion + "' ";
        query += "WHERE " + tabla + ".idPublicity=" + this.ID;
        return this.ExecuteDML(query, "no");
    }

    public int Delete()
    {
        String query = "DELETE FROM " + tabla + " WHERE " + tabla + ".idPublicity={0}";
        String sql = String.Format(query, this.ID);
        return this.ExecuteDML(sql, "no");
    }

    #endregion

    #region Setters and Getters

    
    public int ID
    {
        get { return idPublicity; }
        set { idPublicity = value; }
    }

    public int IdPLegal {
        get { return idPLegal; }
        set { idPLegal = value; }
    }
    
    public String SName
    {
        get { return sName; }
        set { sName = value; }
    }
    
    public String DtDate {
        get { return dtDate; }
        set { dtDate = value; }
    }

    public String SUrlImage {
        get { return sUrlImage; }
        set { sUrlImage = value; }
    }
    public int IdWorkArea {
        get { return idWorkArea; }
        set { idWorkArea = value; }
    }
    public String SRegion {
        get { return sRegion; }
        set { sRegion = value; }
    }

    #endregion

}

#endregion